package com.cotte.flow.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cotte.flow.hbase.dao.GraphTableDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by zhuang on 2018/12/28.
 */
@Service
public class GraphTableService {

    @Autowired
    GraphTableDaoImpl graphTableDao;

    public void updateRow(String key,String workhours,String records){
        graphTableDao.updateRow(key,workhours,records,"");
    }

    /**
     * 工时线性数据
     * <p>Title: lineData</p>
     * <p>Description: </p>
     */
    public ArrayList lineData(String key,String start,String end){
        //String patten = getDays(start,end);
        if(null != start){
            DateFormat dateFormat = new SimpleDateFormat("yyMMdd");
            Date sd = new Date(new Long(start));
            Date ed = null==end ? new Date() : new Date(new Long(end));
            return graphTableDao.getData(key+"_"+dateFormat.format(sd),key+"_"+dateFormat.format(ed));
        }else{
            return graphTableDao.filterData(key+".*");
        }
    }

    /**
     * 统计一天数据
     * <p>Title: countDayData</p>
     * <p>Description: </p>
     */
    public void countDayData(Date date){
        String day = new SimpleDateFormat("yyMMdd").format(date);
        JSONObject obj = new JSONObject();
        JSONArray hours;
        //
        ArrayList list = getTest(day+".*"); // brushcardWorkhoursDao.
        for(int i=0;i<list.size();i++){
            Map<String,String> one = (Map) list.get(i);
            String key = one.get("stepcode")+"_"+one.get("gyzh")+"_"+one.get("fabrictype");
            if(!obj.containsKey(key)){
                obj.put(key,new JSONArray());
            }
            hours = obj.getJSONArray(key);
            Integer val = null==one.get("time")?0:Integer.valueOf(one.get("time"));
            hours.add(val);
        }
        //
        Set<String> it = obj.keySet();
        for(String key : it){
            JSONArray records = obj.getJSONArray(key);
            if(records.size()>0){
                graphTableDao.addDayHours(key+"_"+day,records.toString());
            }
        }
    }

    /**
     * 统计昨天数据
     * <p>Title: countDayData</p>
     * <p>Description: </p>
     */
    public void countYestdayData(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1); //得到前一天
        Date date = calendar.getTime();
        countDayData(date);
    }

    /**
     * 统计历史数据
     * <p>Title: countDayData</p>
     * <p>Description: 根据records筛选有效值计算workhours</p>
     */
    public void countAllData(String isall){
        graphTableDao.addAllHours(isall);
    }


    /**
     * 工具方法: 获取每天的时间,用"|"分割
     */
    public String getDays(String startTime, String endTime) {
        DateFormat dateFormat = new SimpleDateFormat("yyMMdd");
        Date start = new Date(new Long(startTime));
        Date end = new Date(new Long(endTime));

        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        String days = "";
        while (tempStart.before(tempEnd)) {
            days = (days.equals("")?"":"|") + dateFormat.format(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return days;
    }

    public ArrayList getTest(String filter){
        ArrayList result = new ArrayList();
        Map<String,String> one = new HashMap<String,String>();
        one.put("key", "xxx");
        one.put("fabrictype", "条纹");
        one.put("gyzh", "05C2,04N1");
        one.put("odid", "xxx");
        one.put("stepcode", "101");
        one.put("syscode", "xxx");
        one.put("time", "37");
        one.put("loaddate", "180102");
        result.add(one);
        return result;
    }
}
